package com.ruoyi.common.config;

import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import com.ruoyi.common.utils.YamlUtils;

/**
 * 读取项目相关配置
 * 
 * @author ruoyi
 */
@Component
@ConfigurationProperties(prefix = "ruoyi")
public class RuoYiConfig {
    /** 项目名称 */
    private String name;

    /** 版本 */
    private String version;

    /** 版权年份 */
    private String copyrightYear;

    /** 上传路径 */
    private static String profile;

    /** 获取地址开关 */
    private static boolean addressEnabled;

    /** 验证码类型 */
    private static String captchaType;

    // 全局属性值
    private static Map<String, String> MAP_ALL = new HashMap<String, String>();

    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = MAP_ALL.get(key);
        if (value == null) {
            Map<?, ?> yamlMap = null;
            try {
                yamlMap = YamlUtils.loadYaml("application.yml");
                value = String.valueOf(YamlUtils.getProperty(yamlMap, key));
                MAP_ALL.put(key, value != null ? value : "");
                
                if (value == null || value.equals("null") || value.length() == 0) {
                	String activeProfile = String.valueOf(YamlUtils.getProperty(yamlMap, "spring.profiles.active"));
                	Map<?, ?> yamlMap2 = YamlUtils.loadYaml("application-" + activeProfile + ".yml");
                    value = String.valueOf(YamlUtils.getProperty(yamlMap2, key));
                    MAP_ALL.put(key, value != null ? value : "");
        		}
            } catch (FileNotFoundException e) {
            	e.printStackTrace();
            }
        }
        return value;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getVersion()
    {
        return version;
    }

    public void setVersion(String version)
    {
        this.version = version;
    }

    public String getCopyrightYear()
    {
        return copyrightYear;
    }

    public void setCopyrightYear(String copyrightYear)
    {
        this.copyrightYear = copyrightYear;
    }
    
    /**
     * 获取文件存放绝对路径
     */
    public static String getProfile() {
        return getConfig("ruoyi.profile");
    }
    
    /**
     * 获取服务器端口
     */
    public static String getServerPort() {
        return getConfig("server.port");
    }

//    public static String getProfile()
//    {
//        return profile;
//    }

    public void setProfile(String profile)
    {
        RuoYiConfig.profile = profile;
    }

    public static boolean isAddressEnabled()
    {
        return addressEnabled;
    }

    public void setAddressEnabled(boolean addressEnabled)
    {
        RuoYiConfig.addressEnabled = addressEnabled;
    }

    public static String getCaptchaType() {
        return captchaType;
    }

    public void setCaptchaType(String captchaType) {
        RuoYiConfig.captchaType = captchaType;
    }

    /**
     * 获取导入上传路径
     */
    public static String getImportPath()
    {
        return getProfile() + "/import";
    }

    /**
     * 获取头像上传路径
     */
    public static String getAvatarPath() {
        return getProfile() + "/avatar";
    }

    /**
     * 获取评测上传路径
     */
    public static String getEvaluatePath() {
        return getProfile() + "/evaluate";
    }

    /**
     * 获取临时上传路径
     */
    public static String getTempPath() {
        return getProfile() + "/temp";
    }

    /**
     * 获取发音文件上传路径
     */
    public static String getVoicePath() {
        return getProfile() + "/voice";
    }

    /**
     * 获取服务器URL
     */
    public static String getServerUrl()
    {
        return getConfig("server.url");
    }
    /**
     * 获取下载路径
     */
    public static String getDownloadPath()
    {
        return getProfile() + "/download/";
    }

    /**
     * 获取上传路径
     */
    public static String getUploadPath()
    {
        return getProfile() + "/upload";
    }
}
